///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_MAT_DETAIL_CELEMENTUNARYOP_H_
#define _BUOLA_MAT_DETAIL_CELEMENTUNARYOP_H_

#include <buola/mat/cexpr.h>

namespace buola { namespace mat { namespace detail {

template<typename tSub,typename tOp>
struct MMatrixTraits<CElementUnaryOp<tSub,tOp>>
{
private:
    typedef MMatrixTraits<ODecay<tSub>> TSubTraits;

public:
    typedef decltype(std::declval<tOp>()(std::declval<OScalar<tSub>>())) TScalar;
    static const size_t cRows=TSubTraits::cRows;
    static const size_t cCols=TSubTraits::cCols;
    static const bool cRowLinearAccess=TSubTraits::cRowLinearAccess;
    static const bool cColLinearAccess=TSubTraits::cColLinearAccess;
    static const bool cRowPacketAccess=TSubTraits::cRowPacketAccess;
    static const bool cColPacketAccess=TSubTraits::cColPacketAccess;
    static const bool cMightAlias=QIsReference<tSub>::value&&TSubTraits::cMightAlias;
    static const int cElementAccess=constexpr_min(TSubTraits::cElementAccess,(int)ACCESS_SLOW);
    static const bool cEigenMappable=false;
    static const bool cKeepConstness=false;
    static const bool cExprAssignable=false;
    static const bool cIsMatrix=false;
};

//totally fixed size version
template<typename tSub,typename tOp>
class CElementUnaryOp : public CExpr<CElementUnaryOp<tSub,tOp>>
{
    typedef MMatrixTraits<CElementUnaryOp<tSub,tOp>> TSelfTraits;

public:
    CElementUnaryOp(tSub pSub,tOp pOp)
        :   mSub(pSub)
        ,   mOp(pOp)
    {
    }

    CElementUnaryOp(const CElementUnaryOp&)=delete;
    CElementUnaryOp& operator=(const CElementUnaryOp&)=delete;

    size_t Rows() const  {   return mSub.Rows();  }
    size_t Cols() const  {   return mSub.Cols();  }
    size_t size() const  {   return mSub.size();  }

    template<typename tType>
    tType At(size_t r,size_t c) const
    {
        return mOp(mSub.template At<OScalar<tSub>>(r,c));
    }

    template<typename tType>
    tType At(size_t i) const
    {
        return mOp(mSub.template At<OScalar<tSub>>(i));
    }

    //left side can be element-accessed
    template<typename tDst>
    void EvalTo(tDst &pDst) const
    {
        ///\todo consider posible aliasing, and whether it should be solved here or at an outer level
        mSub.EvalTo(pDst);
        
        int lRows=pDst.Rows();
        int lCols=pDst.Cols();
        for(int r=0;r<lRows;r++)
            for(int c=0;c<lCols;c++)
                pDst.template At<OScalar<tDst>>(r,c)=mOp(pDst.template At<OScalar<tSub>>(r,c));
    }

private:
    tSub mSub;
    tOp mOp;
};

template<typename tSub,typename tOp>
using OElementUnaryOpResult=CElementUnaryOp<OElementAccessible<tSub,ACCESS_ONCE>,tOp>;

/*namespace detail*/ } /*namespace mat*/ } /*namespace buola*/ }

#endif
